package com.yunduo.world.utils;

import com.google.protobuf.ByteString;
import com.yunduo.world.constant.NetWorkRoomConstant;
import com.yunduo.world.entity.EntityInfo;
import com.yunduo.world.entity.NetRoom;
import com.yunduo.world.constant.NetWorkConstant;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 网络房间管理
 */
@Component
public class NetRoomUtils {
    private static List<NetRoom> rooms = new ArrayList<>();

    // 创建房间
    public static synchronized boolean add(NetRoom netRoom){
        rooms.add(netRoom);
        return true;
    }

    // 根据id删除房间数据
    public static synchronized boolean delete(String id){
        Iterator<NetRoom> iterator = rooms.iterator();
        while (iterator.hasNext()) {
            NetRoom room = iterator.next();
            if (room.getRoomMaster().equals(id)) {
                iterator.remove();
            }
        }
        return true;
    }

    // 根据id查询
    public static NetRoom get(String id){
        Iterator<NetRoom> iterator = rooms.iterator();
        while (iterator.hasNext()) {
            NetRoom room = iterator.next();
            if (room.getRoomMaster().equals(id)) {
                return room;
            }
        }
        return null;
    }

    // 查找所有房间
    public static List<NetRoom> getAll(){
        return rooms;
    }

    // 修改房间
    public static boolean updateById(NetRoom room) {
        Iterator<NetRoom> iterator = rooms.iterator();
        while (iterator.hasNext()) {
            NetRoom temp = iterator.next();
            if (temp.getRoomMaster().equals(room.getRoomMaster())) {
                // 更新同房间列表
                if (null != room.getRoomVisitor()) {
                    temp.setRoomVisitor(room.getRoomVisitor());
                }
                // 更新房间场景
                if (null != room.getRoomData()) {
                    temp.setRoomVisitor(room.getRoomVisitor());
                }
                return true;
            }
        }
        return false;
    }

    // 用户下线关闭房间
    public static synchronized int deleteRoom(String id) {
        NetRoom netRoom = NetRoomUtils.get(id);
        if (null != netRoom) {
            // 获取所有用户
            Set<Map.Entry<Channel, String>> channels = ClientUtils.entrySet();
            // 根据房间id找到对应channel通知主机下线
            netRoom.getRoomVisitor().forEach( item -> {
                for (Map.Entry<Channel,String> user: channels) {
                    if (user.getValue().equals(item)) {
                        EntityInfo.Entity entity = EntityBuild.buildMessage(NetWorkRoomConstant.ROOM,NetWorkConstant.NET_MASTER_OFF_LINE, NetWorkConstant.NOT_OK);
                        user.getKey().writeAndFlush(entity);
                    }
                }
            });
        }
        rooms.remove(netRoom);
        return 1;
    }

    // 房间是否为空
    public static boolean roomIsNotEmpty(NetRoom netRoom){
        if (null != netRoom && null != netRoom.getRoomData()){
            return true;
        }
        return false;
    }

    // 获取广播列表
    public static List<String> radioList(String sendId) {
        List<String> user_ids= new ArrayList<>();
        NetRoom netRoom = get(sendId);
        if (null != netRoom){
            return netRoom.getRoomVisitor();
        }
        for (NetRoom room :rooms){
            for (String item:room.getRoomVisitor()){
                if (item.equals(sendId)){
                    return findOtherUser(sendId, room);
                }
            }
        }
        return user_ids;
    }

    // 查找非本人id
    public static List<String> findOtherUser(String id,NetRoom room){
        List<String> user_ids= new ArrayList<>();
        if (!id.equals(room.getRoomMaster())){
            user_ids.add(room.getRoomMaster());
        }
        for (String temp:room.getRoomVisitor()){
            if (!id.equals(temp)){
                user_ids.add(temp);
            }
        }
        return user_ids;
    }
}
